<?php
/**
 * 品牌控制器
 * @author 妮妮 <962611024@qq.com>
 */
class BrandController extends AuthController{
	private $model;
	public function __init(){
	    parent::__init();
		$this->model=K('Brand');
	}
    /**
     * 默认访问方法
     */
    public function index(){
    	if(IS_POST){
    		$sorts=Q('post.sort');
			foreach ($sorts as $bid => $sort) {
				$this->model->where("bid={$bid}")->update(array('sort'=>$sort));
			}
			$this->success('排序成功');
    	}
    	//查询所有的数据
    	$data=$this->model->all();
		//分配出去
		$this->assign('brandData',$data);
        $this->display(); 
    }
	/*
	 * 添加
	 * */
	public function add(){
		//当用户提交的时候
		if(IS_POST){
			//p(Q("post."));die;
			$upload = new upload();
			$uploadfiles =$upload->upload();
//			p($uploadfiles);die;
//			如果上传错误，输出错误信息
			if(!$uploadfiles) $this->error($upload->error);
			//如果验证失败，提示错误信息（$model->error会在模型create之后把错误压进去）
			if(!$this->model->addBra($uploadfiles[0]['path'])) $this->error($this->model->error);
			$this->success('添加成功',U('index'));
		}
		
	    $this->display();
	}
	/*
	 * 编辑
	 * */
	public function edit(){
		//查找旧数据（因为下面有用到，所以放到上面）
		$bid=Q('get.bid',0,'intval');
		$oldData=$this->model->where("bid={$bid}")->find();
		$this->assign('oldData',$oldData);
		//p($oldData);
		if(IS_POST){
			//实例化（框架自带）
			$upload  = new upload();
			$uploadfiles = $upload->upload();
			if($uploadfiles){
				//删除旧的图片
				unlink($oldData['logo']);
				$path=$uploadfiles[0]['path'];
			}else{
				$path=	$oldData['logo'];
			}
//			p($uploadfiles);exit;
			if(!$this->model->editBra($path)) $this->error($this->model->error);
			$this->success('修改成功',U('index'));
		}	
		
	    $this->display();
	}
	
	/*
	 * 删除
	 * */
	public function del(){
		//bid获取
		$bid=Q('get.bid',0,'intval');
		//图片路径
		$imgPath=$this->model->where("bid={$bid}")->find();
		if($imgPath['logo']){
			unlink($imgPath['logo']);
		}
		//删除
		$this->model->where("bid={$bid}")->delete();
		$this->success('删除成功');
		
	}
	 
	 
	 
	 
	 
	 
 }
 
 
 
 
 
 
 
 
 
 
 
















 ?>